package com.sdtv.mediunfusion.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

/**
 * 自定义日历的 MonthView
 */
public class MyMonthView extends MonthView {

    private static final String TAG = "MyMonthView";

    /**
     * 自定义魅族标记的文本画笔
     */
    private Paint mTextPaint = new Paint();
    /**
     * 自定义魅族标记的圆形背景
     */
    private Paint mSchemeBasicPaint = new Paint();
    private int mPadding;

    public MyMonthView(Context context) {
        super(context);

        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setFakeBoldText(true);
        mPadding = 2;

    }

    /**
     * 绘制选中的日子
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return 返回true 则会继续绘制onDrawScheme，因为这里背景色不是是互斥的，
     * 所以返回true，返回false，则点击scheme标记的日子，
     * 则不继续绘制onDrawScheme，自行选择即可
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        // 选中的日子----
        mSelectTextPaint.setColor(Color.parseColor("#661A8CF7"));
        canvas.drawCircle(x + mItemWidth / 2 + mPadding, y + mItemHeight / 3 + mPadding, dipToPx(getContext(), 15), mSelectTextPaint);
        if ("0".equals(calendar.getScheme())) {
            mSelectTextPaint.setColor(Color.parseColor("#FF53D769"));
        } else {
            mSelectTextPaint.setColor(Color.parseColor("#FFFF7247"));
        }
        canvas.drawCircle(x + mItemWidth / 2 + mPadding, y + mItemHeight / 3 + mPadding, dipToPx(getContext(), 13), mSelectTextPaint);
        return false;
    }

    /**
     * 绘制标记的事件日子
     *
     * @param canvas   canvas
     * @param calendar 日历calendar
     * @param x        日历Card x起点坐标
     * @param y        日历Card y起点坐标
     */
    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        if ("0".equals(calendar.getScheme())) {
            mSchemeBasicPaint.setColor(Color.parseColor("#FF53D769"));
        } else {
            mSchemeBasicPaint.setColor(Color.parseColor("#FFFF7247"));
        }
        canvas.drawCircle(x + mItemWidth / 2, y + mItemHeight * 3 / 4, dipToPx(getContext(), 3), mSchemeBasicPaint);
    }

    /**
     * 绘制文本
     *
     * @param canvas     canvas
     * @param calendar   日历calendar
     * @param x          日历Card x起点坐标
     * @param y          日历Card y起点坐标
     * @param hasScheme  是否是标记的日期
     * @param isSelected 是否选中
     */
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = y - mItemHeight / 6;
        // 绘制普通文字
        canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                calendar.isCurrentDay() ? mCurDayTextPaint :
                        calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
